//
//  UIImage+Extention.m
//  QQ聊天01
//
//  Created by 郝俊聪 on 15/3/17.
//  Copyright (c) 2015年 heima. All rights reserved.
//

#import "UIImage+Extention.h"

@implementation UIImage (Extention)

/**
 *  返回中心拉伸的图片，保证图片的四周拉伸不变形
 *
 *  @return  返回中心拉伸的图片
 */
- (UIImage *)resizeableImage{
    return [self stretchableImageWithLeftCapWidth:self.size.width/2 topCapHeight:self.size.height/2];
}
/**
 *  返回中心拉伸的图片，保证图片的四周拉伸不变形
 *
 *  @param imageName 图片名字
 *
 *  @return 返回中心拉伸的图片
 */
+ (UIImage *)resizeableImageWithImageName:(NSString *)imageName{
    return [[UIImage imageNamed:imageName] resizeableImage];
}


/**
 *  返回水印图片
 *
 *  @param bgImageName    背景图片名称
 *  @param waterImageName 水印图片名称
 *
 *  @return 返回一个加水印的背景图
 */
+ (instancetype)waterImageWithBgImageName:(NSString *)bgImageName andWaterImageName:(NSString *)waterImageName{
    //加载背景图片
    UIImage *image = [UIImage imageNamed:bgImageName];
    
    //获得位图上下文
    UIGraphicsBeginImageContextWithOptions(image.size, 0, 0.0);
    
    
    //将背景图片画入
    [image drawAtPoint:CGPointMake(0, 0)];
    
    //加载水印图片
    UIImage *waterImg = [UIImage imageNamed:waterImageName];
    //设置水印区域的大小坐标
    CGFloat waterW = waterImg.size.width;
    CGFloat waterH = waterImg.size.height;
    CGFloat margin = 5;
    CGFloat waterX = image.size.width - waterW + margin;
    CGFloat waterY = image.size.height - waterH - margin;
    //绘制水印
    [waterImg drawInRect:CGRectMake(waterX, waterY, waterW, waterH)];

    
    //从位图上下文取图片
    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //结束位图上下文
    UIGraphicsEndImageContext();
    
    return finalImage;
}


/**
 *  将图片剪成圆形
 *
 *  @param imageName 原图片名字
 *
 *  @return 返回圆形图片
 */
+ (instancetype)clipCircleImageWithImageName:(NSString *)imageName{
    //加载原图
    UIImage *image = [UIImage imageNamed:imageName];
    
    //获得位图上下文
    UIGraphicsBeginImageContextWithOptions(image.size, 0, 0.0);
    
    //*****************************裁剪***********************
    //获得当前的图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    //画圆
    CGFloat  centerX = image.size.width/2;
    CGFloat  centerY = image.size.height/2;
    CGFloat  radius = MIN(image.size.width/2, image.size.height/2);
    
    CGContextAddArc(ctx, centerX, centerY, radius, 0, M_PI * 2, 0);
    
    //剪裁
    CGContextClip(ctx);
    //************************************
    
    //将图片画到位图上下文
    [image drawAtPoint:CGPointMake(0, 0)];
    
    //从位图上下文获取图片
    
    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //结束位图上下文
    UIGraphicsEndImageContext();
    
    return finalImage;
}


/**
 *  剪裁成圆形图片，并带上圆环
 *
 *  @param imageName    原始图片名称
 *  @param boarderWidth 圆环厚度
 *  @param color        圆环颜色
 *
 *  @return 返回才圆边的图片
 */
+ (instancetype)clipCircleImageWithImageName:(NSString *)imageName boarderWidth:(CGFloat)boarderWidth andBoarderColor:(UIColor *)color{
    //加载图片
    UIImage *img = [UIImage imageNamed:imageName];
    
    CGFloat bigSizeX = img.size.width + boarderWidth * 2;
    CGFloat bigSizeY = img.size.height + boarderWidth * 2;
    //开启位图上下文
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(bigSizeX, bigSizeY), 0, 0);
    
    //获得当前图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    //画大圆
    CGFloat bigRadius = MIN(img.size.width/2, img.size.height/2)+boarderWidth;
    CGContextAddArc(ctx, bigSizeX/2, bigSizeY/2, bigRadius, 0, M_PI*2, 0);
    
    //设置大圆的颜色
    [color set];
    
    //渲染
    CGContextFillPath(ctx);
    
    
    //画小圆
    CGFloat smallRadius = MIN(img.size.width, img.size.height)/2;
    CGContextAddArc(ctx, bigSizeX/2, bigSizeY/2, smallRadius, 0, M_PI * 2, 0);
    
    //剪裁
    CGContextClip(ctx);
    
    //将图片画入小圆中
    [img drawAtPoint:CGPointMake(boarderWidth,boarderWidth)];
    
    
    //从位图上下文获得图片
    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
    
    //结束位图上下文
    UIGraphicsEndImageContext();
    
    return finalImage;
}
/**
 *  截屏
 *
 *  @param view 要保存的屏幕上得view
 *
 *  @return 返回截屏图片
 */
+ (instancetype)imageOfSaveScreenWithView:(UIView *)view{
    //开启位图上下文
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, 0, 0);
    
    //获得当前图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    //将要截图的view的layer绘制到上下文中
    [view.layer renderInContext:ctx];
    
    //从位图上下文获得图片
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    
    //结束位图上下文
    UIGraphicsEndImageContext();

    return img;
}

- (instancetype)imageOfScaleWithWidth:(CGFloat)width{
    CGFloat height = width /(self.size.width/self.size.height);
    
   //开启图形上下文
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(width,height), 0, 0);
//    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [self drawInRect:CGRectMake(0,0,width,height)];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    //关闭图形上下文
    UIGraphicsEndImageContext();
    
    return image;
    
}

@end
